#!/usr/bin/python
#***************************************************************************
# Copyright notice for this file:
#  Copyright (C) 2012 TheAxeMan
#  _______ _                               _    _
# |__   __| |     ___     /\          ___ | \  / |
#    | |  | |__  / _ \   /  \  __  _ / _ \|  \/  | __ _ _ __
#    | |  | '_ \|  __/  / /\ \ \ \/ |  __/| .  . |/ _` | '_ \
#    | |  | | | | (__/\/ ____ \ )  (| (__/| |\/| | (_| | | | |
#    |_|  |_| |_|\____/_/    \_._/\_.\____|_|  |_|\__,_|_| |_|
#
# Final Fantasy TAS scripts by TheAxeMan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#***************************************************************************

import sys
import re
import string

#need this for area name translation
from gamedata import GameData

"""
One segment of a result. Has some number of steps and then
possibly an battle or a note. It might just have the note
if steps is invalid.
"""
class ResultSegment:
  def __init__(self, area_id, steps, enemy_group_id=-1, note=""):
    self.area_id = area_id
    self.steps = steps
    self.enemy_group_id = enemy_group_id
    self.note = note
  def isNote(self):
    return(self.steps <= 0 and self.note != "")
  def hasEncounter(self):
    return (self.enemy_group_id >= 0)

class ResultSegments:
  def __init__(self):
    self.results = []

  def addSegment(self, segment):
    self.results.append(segment)
  def addSegments(self, segments):
    self.results += segments.results
  def addNote(self, note):
    self.results.append(ResultSegment(0, 0, -1, note))

  def totalSteps(self):
    total = 0
    for segment in self.results:
      if segment.steps >= 0:
        total += segment.steps
    return total

  def totalEncounters(self):
    total = 0
    for segment in self.results:
      if segment.enemy_group_id >= 0:
        total += 1
    return total

  def noRunFightCount(self, gdata):
    total = 0
    for segment in self.results:
      enemy_group_name = gdata.enemy_groups[segment.enemy_group_id]
      if enemy_group_name in gdata.noRunList:
        total += 1
    return total

  def shadowFightCount(self, gdata):
    total = 0
    for segment in self.results:
      enemy_group_name = gdata.enemy_groups[segment.enemy_group_id]
      if enemy_group_name in gdata.shadowList:
        total += 1
    return total

  def compareToFightList(self, fight_list):
    fight_index = 0
    if len(fight_list) != self.totalEncounters():
      print "mismatch in length:",len(fight_list),"fights in fight list,",self.totalEncounters(),"fights in results"
    for segment in self.results:
      if segment.hasEncounter():
        if segment.enemy_group_id != fight_list[fight_index]:
          print "mismatch on fight",fight_index
          print "fight list has",fight_list[fight_index],"and result has",segment.enemy_group_id
          return False
        else:
          fight_index += 1
    print "fight list matches results"
    return True


  def getReport(self, gdata):
    result_string = ""
    for segment in self.results:
      if segment.isNote():
        result_string += "-*- "+segment.note + "\n"
        continue
      if segment.steps > 0:
        area_name = gdata.areas[segment.area_id]
        result_string +=  str(segment.steps) + " steps in " + area_name + "\n"
      if segment.hasEncounter():
        enemy_group_name = gdata.enemy_groups[segment.enemy_group_id]
        enemies_in_group = gdata.group_info[enemy_group_name]
        enemies_in_group_string = ", ".join(enemies_in_group)
        result_string +=  "  fight " + enemy_group_name + " - " + enemies_in_group_string + "\n"
        if enemy_group_name in gdata.noRunList:
          result_string +=  "  ***can't run***" + "\n"
        elif enemy_group_name in gdata.shadowList:
          result_string +=  "  ***shadows!!***" + "\n"
      if segment.note != "":
        result_string +=  "-*- "+segment.note + "\n"
    return result_string
      


